import { ROUTE_WHITE_LIST } from './router.whiteList';
import { useToken } from '@/hooks/useToken';
import store from '@/store';
import {onShow} from '@dcloudio/uni-app';
import global from '@/global';

// const store = useStore();
// console.log('store=====>',store)

interface IRouteInterceptor {
	beforeEach: (route: IRouteBeforeEachResult) => IRouteBeforeEachResult | boolean;
	afterEach: (result: UniNamespace.NavigateToSuccessOptions) => UniNamespace.NavigateToSuccessOptions;
}



interface IRouteBeforeEachResult {
	url: string;
}

const interceptor: IRouteInterceptor = {
	beforeEach: (route) => {
		const url: string = route.url ? route.url.split("?")[0] : '';
		console.log('路由前置守卫===>', url)
		const { getTokenSync } = useToken();
		if (url&&ROUTE_WHITE_LIST.indexOf(url) == -1 && (!getTokenSync() || !store.state.userInfo)) {
			console.log('无登录状态进入页面');
			global.showModel();
			return false
		}
		//在此登录验证，路由白名单验证
		return route
	},
	afterEach: (route) => {
		console.log('后置路由首位===>',route)
		return route
	}
}

export default interceptor